package fr.utbm.gi.vi51.slizers.pw.gui.menu;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
import java.beans.PropertyChangeEvent;
import java.io.File;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * @author Adrien
 * 
 * Implementation of a GUI Panel permitting to select a map in the {@link MenuPanel}
 *
 */
public class MapSelectorPanel extends JPanel
{

	/**
	 * Version number associated to the serializable class.
	 */
	private static final long serialVersionUID = -854764867574163040L;
	
	/**
	 * The list containing all the map stored in the directory "current/maps"
	 */
	private JList 	mapList;
	
	private JLabel	mapIcone;

	/**
	 * Constructor
	 */
	public MapSelectorPanel()
	{
		this.setLayout( new BorderLayout() );
		
		// Label for the list
		JLabel label = new JLabel("Select your map");
		this.add( label, BorderLayout.NORTH );
		
		this.mapList = new JList();
		this.mapList.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
		
		// Fill the list
		fillMapList();
		
		// Add a Listener to change the image
		this.mapList.addListSelectionListener( new ListSelectionListener()
		{
			@Override
			public void valueChanged( ListSelectionEvent evt )
			{
				String url = ( String )MapSelectorPanel.this.mapList.getSelectedValue();
				if( url != null )
				{
					url = System.getProperty( "user.dir" ).concat( "/maps/" ) + 
						url.substring( 0, url.length() - 3 ) + "jpg"; //$NON-NLS-1$

					ImageIcon icon = new ImageIcon( url );
					icon.setImage( icon.getImage().getScaledInstance( 500,
			        		500*icon.getIconHeight() / icon.getIconWidth(), 0 ) );
			        MapSelectorPanel.this.mapIcone.setIcon( icon );
				}
			}
		});
		
		// Add component in the layout
		this.add( this.mapList, BorderLayout.WEST );
		//Add the icone
		this.mapIcone = new JLabel();
		this.add( this.mapIcone, BorderLayout.CENTER );
		// Add a scroll bar
		JScrollPane scroller = new JScrollPane( this.mapList );
		this.add( scroller, BorderLayout.WEST );
	}
	
	/**
	 * Fill the list with the maps stored in the directory "current/maps"
	 */
	public void fillMapList()
	{
		DefaultListModel mapDLM = new DefaultListModel();

		String mapDirectoryPath = System.getProperty( "user.dir" ).concat( "/maps/" );  //$NON-NLS-1$//$NON-NLS-2$

		File mapDirectory = new File(mapDirectoryPath);
		
		File[] mapFiles = mapDirectory.listFiles();
		
		if( mapFiles == null )
			JOptionPane.showMessageDialog( this, "the maps directory is not found\n" + //$NON-NLS-1$
					"Please resolve this problem and restart the application" ); //$NON-NLS-1$
		else
		{
			// Browse the map files and add the .txt files
			for (int i = 0; i < mapFiles.length; i++)
			{
				if( mapFiles[i].getName().endsWith( ".txt" ) ) //$NON-NLS-1$
					mapDLM.addElement( mapFiles[i].getName() );
			}		
			
			this.mapList.setModel(mapDLM);
		}
	}
	
	/**
	 * Returns the selected map path.
	 * 
	 * @return the selected map path
	 */
	public String getSelectedMapPath() 
	{
		return ( String )this.mapList.getSelectedValue();
	}

}
